﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows.Controls;
using CP.NLayer.Client.WpfClient.Common;
using CP.NLayer.Models.Business;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Unity;

namespace CP.NLayer.Client.WpfClient.Modules.Admin.Role
{
    /// <summary>
    /// Interaction logic for RoleDisplayView.xaml
    /// </summary>
    public partial class RoleDisplayView : UserControl, IRegionMemberLifetime
    {
        public RoleDisplayView()
        {
            InitializeComponent();
            this.DataContext = DependencyInjection.Container.Resolve<DisplayViewModel<RoleDisplayModel>>();
            ((DisplayViewModel<RoleDisplayModel>)this.DataContext).EditViewType = typeof(RoleEditView);

            //this.radGridView.SelectedItems is readonly, can not set binding to it.
            this.radGridView.SelectionChanged += new EventHandler<Telerik.Windows.Controls.SelectionChangeEventArgs>(RadGridView_SelectionChanged);
        }

        /// <summary>
        /// Indicates whether to create a new view every time navigation occurs in PRISM
        /// </summary>
        public bool KeepAlive
        {
            get { return false; } 
        }

        private void RadGridView_SelectionChanged(object sender, Telerik.Windows.Controls.SelectionChangeEventArgs e)
        {
            var tempList = new List<object>();

            if (radGridView.SelectedItems != null)
            {
                foreach (var item in radGridView.SelectedItems)
                {
                    tempList.Add(item);
                }
            }
            ((DisplayViewModel<RoleDisplayModel>)this.DataContext).SelectedItems = new ObservableCollection<object>(tempList);
        }
    }
}
